$(function () {

    //表格
    var product_datagrid = $("#member_datagrid");
    //弹窗
    var product_dialog = $("#product_dialog");
    //表单
    var product_add_form = $("#product_add_form");


    //数据表格渲染
    product_datagrid.datagrid({
        url: "/member/list",
        fit: true,
        fitColumns: true,
        border: false,
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        toolbar: '#product_datagrid_toolbar',
        columns: [[
            {field: 'cardSn', title: '卡号', width: 100},
            {field: 'username', title: '姓名', width: 100},
            {field: 'qq', title: 'qq', width: 100},
            {field: 'email', title: '邮箱', width: 100},
            {field: 'birthday', title: '生日', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'appellation', title: '称谓', width: 100},
            {field: 'wechat', title: '会员微信', width: 100},
            {field: 'address', title: '会员住址', width: 100},
            {field: 'interest', title: '会员兴趣', width: 100},
            {field: 'remark', title: '备注', width: 100},
            {field: 'tips', title: '提醒', width: 100},
            {field: 'deadLine', title: '过期时间', width: 100},
            {field: 'accumulation', title: '积分', width: 100},
            {field: 'state', title: '状态', width: 100},
            {field: 'level', title: '等级', width: 100},
            {field: 'group', title: '分组', width: 100},
            {field: 'recommender', title: '推荐人', width: 100},
            {field: 'resource', title: '来源', width: 100},
            {field: 'shop', title: '所属分店', width: 100},
            {field: 'totalAccount', title: '账户总金额', width: 100},
            {field: 'giftAccount', title: '赠送金额', width: 100}
        ]]
    });
    //弹窗渲染
    product_dialog.dialog({
        title: "添加",
        buttons: '#product_dialog_buttons',
        closed: true

    });

    //1.把方法绑定到一个对象上
    var cmdObj = {
        //表格导出
        export: function () {
            window.location.href = "/product/export";
        },
        //高级查询
        search: function () {
            //1.获取查询input里面的值
            var keyword = $("#keyword").val();

            //2.调用datagrid重新加载数据,并且带上查询条件  keyword
            product_datagrid.datagrid("load", {keyword: keyword});
        },
        //员工添加
        addproduct: function () {
            //清空表单
            product_add_form.form("clear");

            //显示添加弹窗
            product_dialog.dialog("setTitle", "添加");
            product_dialog.dialog("open");
        },
        //员工删除
        removeproduct: function () {
            //获取选中行
            var selectedRow = product_datagrid.datagrid("getSelected");

            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "老铁,请选择一行数据", "error");
                return;
            }
            //给出提示确认要删除吗
            $.messager.confirm("提示", "老铁,您确定要离职 [ " + selectedRow.realName + " ]员工吗?", function (yes) {
                if (yes) {

                    //发送ajax请求执行删除
                    $.post("/product/delete", {id: selectedRow.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                cmdObj.reloadEmp();
                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }

                    });
                }
            })
        },
        //员工编辑
        editproduct: function () {
            //获取选中行
            var selectedRow = product_datagrid.datagrid("getSelected");

            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "老铁,请选择一行数据", "error");
                return;
            }
            //设置表单回显
            //清空表单
            product_add_form.form("clear");

            //处理部门回显
            if (selectedRow.dept) {
                selectedRow['dept.id'] = selectedRow.dept.id;
            }
            product_add_form.form("load", selectedRow);

            //弹出编辑框
            product_dialog.dialog("setTitle", "编辑");
            product_dialog.dialog("open");
        },
        //员工刷新
        reloadEmp: function () {
            product_datagrid.datagrid("reload");
        },
        //提交保存
        save: function () {
            var url = "/employee/save";

            //获取员工的id
            var productId = $("#product_id").val();
            if (productId) {
                //编辑的url
                url = "/product/update";
            }
            //提交表单
            product_add_form.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadEmp();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            });
        },
        //取消
        cancel: function () {
            //关闭添加弹窗
            product_dialog.dialog("close");
        }

    };

    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

});



